﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FortuneHeavenBAL;
using FortuneHeavenCommon.Enum;
using FortuneHeaven.Admin.Common;

namespace FortuneHeaven.Admin.Pages.CMS
{
    public partial class NewsEdit : FortuneHeaven.Admin.Common.PageBase
    {
        public override string PageID
        {
            get
            {
                if (Type >= 0)
                {
                    NewsType newstype = (NewsType)Type;
                    switch (newstype)
                    {
                        case NewsType.BroadCast:
                            return "F11";
                        case NewsType.Games:
                            return "F12";
                        case NewsType.Competition:
                            return "F13";
                        case NewsType.Others:
                            return "F15";
                        default:
                            break;
                    }
                }
                return "";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            lblDeptName.Text = "游戏道具";
            this.usPager.PagerChanged += new Admin.Controls.Pagination.PagerChangeHandler(usPager_PagerChanged);
            if (!this.IsPostBack)
            {
                if (Type >= 0)
                {
                    NewsType newstype = (NewsType)Type;

                    switch (newstype)
                    {
                        case NewsType.BroadCast:
                            lblDeptName.Text = "新闻公告";
                            break;
                        case NewsType.Games:
                            lblDeptName.Text = "游戏公告";
                            break;
                        case NewsType.Competition:
                            lblDeptName.Text = "赛事公告";
                            break;
                        case NewsType.Others:
                            lblDeptName.Text = "静态页面设置";
                            break;
                        default:
                            break;
                    }
                }
                this.drpPagerSize.Items.Clear();
                this.drpPagerSize.Items.Add(new ListItem("每页10行", "10"));
                this.drpPagerSize.Items.Add(new ListItem("每页20行", "20"));
                this.drpPagerSize.Items.Add(new ListItem("每页50行", "50"));


                if (!string.IsNullOrEmpty(Request["pagesize"]))
                {
                    this.drpPagerSize.SelectedIndex = int.Parse(Request["pagesize"]);
                }
                else
                {
                    this.drpPagerSize.SelectedValue = "10";
                }
                if (!string.IsNullOrEmpty(Request["current"]))
                {
                    ResetList(int.Parse(Request["current"]));
                }
                else
                {
                    ResetList();
                }
                //this.lvNews.ItemCommand += new EventHandler<ListViewCommandEventArgs>(lvNews_ItemCommand);

            }
        }


        public NewsType Type
        {
            get
            {
                return (NewsType)int.Parse(Request["type"]);
            }

        }

        private void usPager_PagerChanged(object sender, int currentpage)
        {
            Refresh();
        }

        public void Refresh()
        {
            NewsBO news = new NewsBO();
            lvNews.DataSource = news.GetNews(this.usPager.CurrentPageIndex, int.Parse(this.drpPagerSize.SelectedValue),Type);
           
            lvNews.DataBind();
        }

        protected void drpPagerSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            ResetList();
        }

        public void ResetList()
        {
            ResetList(1);
        }

        public void ResetList(int current)
        {
            NewsBO news = new NewsBO();

            this.usPager.RecordCount = news.GetTotalSize(Type);
            this.usPager.PageSize = int.Parse(this.drpPagerSize.SelectedValue);
            this.usPager.Initial();
            this.usPager.CurrentPageIndex = current;
        }

        public void AddItem(object sender, EventArgs arg)
        {
            Response.Redirect(string.Format("NewsDetail.aspx?type={0}&pre={1}", ((int)Type).ToString(), "NewsEdit.aspx?type=" + ((int)Type).ToString()));
        }

        public void DeleteItem(object sender, EventArgs arg)
        {
            NewsBO gifts = new NewsBO();
            List<int> ids = new List<int>();
            foreach (ListViewItem item in this.lvNews.Items)
            {
                Control control = item.FindControl("chkSelect");

                if (control != null && control is CheckBox)
                {
                    if ((control as CheckBox).Checked)
                    {
                        ids.Add(int.Parse((item.FindControl("hfIDs") as HiddenField).Value));
                    }

                }
            }

            if (ids.Count > 0)
            {
                gifts.Delete(ids);
                ResetList();
            }
        }
        public void OpenDetail(object sender, EventArgs args)
        {
            LinkButton button = sender as LinkButton;
            Response.Redirect(string.Format("NewsDetail.aspx?id={0}&type={1}&pre={2}", button.CommandArgument, ((int)Type).ToString(), Generatepre()));
        }

        public string Generatepre()
        {
            string ret = string.Format("NewsEdit.aspx?pagesize={0}&current={1}&type={2}", this.usPager.PageSize.ToString(), this.usPager.CurrentPageIndex.ToString(), ((int)Type).ToString());
            return Server.UrlEncode(ret);
        }
    }
}